package com.example.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.DriverManager;

@Component
public class JdbcConfiguration {
    @Value("${jdbc.datasourse.Driver}")
    private  String driver;
    @Value("${jdbc.datasourse.URL}")
    private  String url;
    @Value("${jdbc.datasourse.User}")
    private  String user;
    @Value("${jdbc.datasourse.Password}")
    private String password;
    //返回数据库连接

    public  Connection getConnection(){
        try{
            //注册数据库的驱动
            Class.forName(driver);
            //获取数据库连接（里面内容依次是：主机名和端口、用户名、密码）
            Connection connection = DriverManager.getConnection(url,user,password);

            //返回数据库连接
            return connection;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}




